package com.pfzy.utildemo.activity.im;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hyphenate.chat.EMMessage;
import com.hyphenate.chat.EMTextMessageBody;
import com.pfzy.utildemo.ConstsValue;
import com.pfzy.utildemo.R;

import java.util.Date;

public class SendMessageView extends RelativeLayout {
    private TextView tvTimeStamp;
    private ImageView sendMessageProgress;
    private TextView sendMessage;

    public SendMessageView(Context context) {
        super(context, null);
        View view = View.inflate(context, R.layout.view_send_message_item, this);
        tvTimeStamp = view.findViewById(R.id.timestamp);
        sendMessageProgress = view.findViewById(R.id.sendMessageProgress);
        sendMessage = view.findViewById(R.id.sendMessage);
    }

    public void bindView(Message emMessage) {
        updateMessage(emMessage);
        updateProgress(emMessage);
        updataTimestamp(emMessage);
    }

    private void updataTimestamp(Message emMessage) {
        String timeStr = com.hyphenate.util.DateUtils.getTimestampString(new Date(emMessage.getTime()));
        tvTimeStamp.setText(timeStr);
    }

    private void updateProgress(Message emMessage) {
        int status = emMessage.getStatus();
        if (status == ConstsValue.MESSAGE_STATUS_INPROGRESS) { //发送中
            sendMessageProgress.setVisibility(View.VISIBLE);
            sendMessageProgress.setImageResource(R.drawable.send_message_progress);
            AnimationDrawable animationDrawable = (AnimationDrawable) sendMessageProgress.getDrawable();
            animationDrawable.start();
        } else if (status == ConstsValue.MESSAGE_STATUS_SUCCESS) { //发送成功
            sendMessageProgress.setVisibility(View.GONE);
        } else if (status == ConstsValue.MESSAGE_STATUS_FAIL) {//发送失败
            sendMessageProgress.setVisibility(View.VISIBLE);
            sendMessageProgress.setImageResource(R.mipmap.msg_error);
        }
    }

    private void updateMessage(Message emMessage) {
//        if (emMessage.getType() == EMMessage.Type.TXT) {
//            EMTextMessageBody body = (EMTextMessageBody) emMessage.getBody();
        sendMessage.setText(emMessage.getMessage());
//        } else {
//            // 其他类型
//            sendMessage.setText(getContext().getString(R.string.no_text_message));
//        }
    }
}
